home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / networke / xfirepow.000 / xfirepow / xfirepower-0.84 / client / oldmap.c < prev    next >
C/C++ Source or Header  |  1995-05-23  |  2KB  |  119 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "Wlib.h"
  5. #include "data.h"
  6. #include "defs.h"
  7. #include "struct.h"
  8. #include "proto.h"
  9.  
  10. /* wall shape based on neighbors */
  11. #define W_TOP 1
  12. #define W_RIGHT 2
  13. #define W_BOT 4
  14. #define W_LEFT 8
  15.  
  16. int wallshapes[16] = {
  17.     T_POST,
  18.     T_WALL_T,
  19.     T_WALL_R,
  20.     T_WALL_TR,
  21.     T_WALL_B,
  22.     T_WALL_TB,
  23.     T_WALL_BR,
  24.     T_WALL_TBR,
  25.     T_WALL_L,
  26.     T_WALL_TL,
  27.     T_WALL_RL,
  28.     T_WALL_TRL,
  29.     T_WALL_BL,
  30.     T_WALL_TBL,
  31.     T_WALL_BRL,
  32.     T_WALL_TBRL,
  33. };
  34.  
  35. int iswall(char v)
  36. {
  37.     if(v>=T_GWALL && v<= T_WALL_TBRL)
  38.     return 1;
  39.     return 0;
  40. }
  41.  
  42. int
  43. read_map(char *name)
  44. {
  45.     FILE *mf;
  46.     int w,h,x,y;
  47.     char buf[512];
  48.     int walls;
  49.  
  50.     if(!(mf = fopen(name, "r"))) {
  51.     fprintf(stderr, "Can't open map file %s\n", name);
  52.     return 0;
  53.     }
  54.     
  55.     fgets(buf, 512, mf);
  56.     strncpy(map_info.m_name, buf, 32);
  57.     if(strlen(buf) < 32)
  58.     map_info.m_name[strlen(buf)-1] = 0;
  59.     else
  60.     map_info.m_name[31] = 0;
  61.  
  62.     fprintf(stderr, "Map name is %s\n", map_info.m_name);
  63.  
  64.     fgets(buf, 512, mf);
  65.     if(sscanf(buf, "%d %d", &w, &h) != 2) {
  66.     fprintf(stderr, "Error parsing width and height from %s", buf);
  67.     fclose(mf);
  68.     return 0;
  69.     }
  70.     map_info.m_width = w;
  71.     map_info.m_height = h;
  72.  
  73.     for(y=0;y<h;y++) {
  74.     if(!fgets(buf, 512, mf)) {
  75.         perror("read_map");
  76.         fclose(mf);
  77.         return 0;
  78.     }
  79.     for(x=0;x<w;x++) {
  80.         switch(buf[x]) {
  81.           case '.':
  82.         map[x][y] = T_GRASS;
  83.         break;
  84.           case 'r':
  85.         map[x][y] = T_ROAD;
  86.         break;
  87.           case 't':
  88.         map[x][y] = T_TREE;
  89.         break;
  90.           case 'w':
  91.         map[x][y] = T_GWALL;
  92.         break;
  93.         }
  94.     }
  95.     }
  96.  
  97.     fclose(mf);
  98.  
  99.     /* post process */
  100.     for(y=0;y<h;y++) {
  101.     for(x=0;x<w;x++) {
  102.         if(map[x][y] == T_GWALL) {
  103.         walls=0;
  104.         if(iswall(map[x][y-1]))
  105.             walls |= W_TOP;
  106.         if(iswall(map[x+1][y]))
  107.             walls |= W_RIGHT;
  108.         if(iswall(map[x][y+1]))
  109.             walls |= W_BOT;
  110.         if(iswall(map[x-1][y]))
  111.             walls |= W_LEFT;
  112.         map[x][y] = wallshapes[walls];
  113.         }
  114.     }
  115.     }
  116.     return 1;
  117. }
  118.  
  119.